
<?php
class Item extends Vnf_Db_Table{
    protected $_name;
    protected $_primary = 'item_id';

    function __construct() {
        $this->_name = DB_PREFIX . "item";
        parent::__construct(); 
    }

    public function getColumns() {
        $results = array('item_id', 'item_title', 'category_id', 'item_image', 'item_short_description', 'item_description', 'item_order', 'item_status', 'top_product', 'capbac_id');
        return $results;
    }

    public function getPrimaryKey() {
        $pk = $this->_primary;
        if (is_array($pk)) {
            if (count($pk) > 0)
                return $pk[1]; // array[1] contain column name of primary key
        }
        return $this->_primary;
    }
    
    public function getRecords($where = null, $order = null, $count = null, $offset = 0) {
        $table_name = $this->_name;
        $primary_key = $this->getPrimaryKey();
        $select = $select = Zend_Db_Table_Abstract::select();
        $select->setIntegrityCheck(false)
            ->from(array('thistable' => $table_name))
            ->joinLeft( array( 'ftable' => DB_PREFIX . "category"), "thistable.category_id = ftable.category_id", array('admin_title'))
            ->joinLeft( array( 'f1table' => DB_PREFIX . "capbac"), "thistable.capbac_id = f1table.capbac_id", array('capbac_name'));
        if($where) {
            $select->where($where);
        }
        if($order) {
            $select->order($order);
        }
        if($count)
            $select->limit($count, $offset);
        $data = $this->fetchAll($select);
        return $data;
    }
    
    public function getItemEachCat($strID, $countItem = 10) {
        $table_name = $this->_name;
        $primary_key = $this->getPrimaryKey();

        $countItem = (int)$countItem;

        if($countItem <= 0)
            $countItem = 10;
        $where = "";
        if ($strID) {
            $where = " WHERE i1.category_id in ($strID)";
        }

        /*$sql = "SELECT i1.*
        FROM " . DB_PREFIX . "item i1
        LEFT OUTER JOIN " . DB_PREFIX . "item i2
          ON (i1.category_id = i2.category_id AND i1.item_id < i2.item_id)
          $where
        GROUP BY i1.item_id
        HAVING COUNT(*) < $countItem
        ORDER BY category_id";

        $data = $this->_db->query($sql);
        
        print_r($data);

        */
        $select = Zend_Db_Table_Abstract::select()
            ->setIntegrityCheck(false)
            ->from(array('i1' => $this->_name))
            ->joinLeft(array('i2' => $this->_name), 'i1.category_id = i2.category_id AND i1.item_id < i2.item_id', array())
            ->join(array('cat' => 'vnf_category'), 'i1.category_id = cat.category_id', array('admin_title', 'left'))
            ->group("i1.item_id")
            ->having("COUNT(*) < $countItem")
            ->order(array('left ASC', 'item_id DESC'));
            
        if ($strID) {
            $select->where("i1.category_id in ($strID)");
        }

        //echo $select;        

        $data = $this->fetchAll($select);
        //$stmt = $select->query();
        //$data = $stmt->fetchAll();

        return $data;
    }
}
?>
